var map;
var poi;

$(function(){

	$("#dg").datagrid({
		onSelect:function(rowIndex,rowData){
			addUser(rowData);
		},
		onUnselect:function(rowIndex,rowData){
			var name = rowData.name;
			var userId = rowData.id;
			removeMarker(userId);
			//alert(markerMap.length);
			//removeUser(new BMap.Point(121.553252, 31.245336), name);
		},
		onSelectAll:function(rows){
			$.each(rows,function(rowIndex,rowData){
				addUser(rowData);
			});
		},
		onUnselectAll:function(rows){
			$.each(rows,function(rowIndex,rowData){
				var name = rowData.name;
				var userId = rowData.id;
				removeMarker(userId);
			});
		}
	});


	// 百度地图API功能
	map = new BMap.Map('allmap');
	poi = new BMap.Point(121.537729, 31.234375);
	map.centerAndZoom(poi, 15);
	map.enableScrollWheelZoom();
	map.addControl(navigationControl);

	//assign task
	$("#assignTaskBtn").click(function(){
		assignTask();
		//window.parent.onAddTask();
	});
});

//assign task to user
function assignTask(){
	var rows = $('#dg').datagrid('getChecked');
	if(rows.length==0){
		messager_show("请选择外勤人员",3000);
		return;
	}

	var _userIdArrStr = '';
	var _userIdArray = [];
	for(var i=0; i<rows.length; i++){
		var row = rows[i];
		var userId = row.id;
		_userIdArrStr = _userIdArrStr+userId+",";
		_userIdArray[i]=userId;
	}

	var url = "/admin123/coding/owTask/map_add_edit.jsp";
	var isDialog = 1;
	var width = 1100;
	var height = 600;
	var itemName = "生成任务";
	openDialog(url,isDialog,width,height,itemName,'生成任务',_userIdArray,function(){
		window.frames['iframe_dialog'].formAddSubmitJustAdd();
	})

}



function openDialog(url,isDialog,width,height,itemName,submitText,_userIdArray,callback){

	var tab_title=window.parent.getSelectedTabTitle();
	var jsonParam = {};
	jsonParam.isDialog = isDialog;
	jsonParam.url = _path+url;
	jsonParam.title = tab_title+"-"+itemName;
	jsonParam.width = width;
	jsonParam.height = height;
	//jsonParam.dialog_style = _style4AddDialog;
	jsonParam.buttons=[];
	jsonParam.buttons[jsonParam.buttons.length]={
		text:submitText,
		iconCls:'icon-save',
		handler:callback
	}
	jsonParam.buttons[jsonParam.buttons.length]={
		text:'关闭',
		iconCls:'icon-cancel',
		handler:function(){
			$.iframe.close();
		}
	}
	jsonParam.parameter1_iframedialog=_userIdArray;
	jsonParam.parameter2_iframedialog="344";
	jsonParam.parameter3_iframedialog="565";
	openUrl(jsonParam);
}


//backup market infomation for remove function
var markerJA = [];

function setTrackListByUser(userId,marker,markerJO){

	// ajax
	var _url = "/springmvc/task/queryUserTrackList.mvc";
	_url = _path + _url;
	var _param = {};
	_param.a2userId = userId;

	var pointList = [];
	$.post(_url,_param,function(data){
		if(data.success!=true){
			messager_show(data.content,3000);
			return;
		}
		$.each(data.resultMap.rows,function(i,val){
			var point = new BMap.Point(val.lng, val.lat);
			pointList.push(point);
		});

		if(pointList.length>0){
			// 设置新的标注位置
			marker.setPosition(pointList[pointList.length-1]);
			map.centerAndZoom(pointList[pointList.length-1], 15);
			map.addOverlay(marker);

			polyline = new BMap.Polyline(pointList, {
				strokeColor: "#DC1118",
				strokeWeight: 3,
				strokeOpacity: 0.7
			});
			//修改移动轨迹
			map.addOverlay(polyline);
			markerJO.polyline=polyline;
		}else{
			messager_show("无坐标信息，可能未签到",3000);
		}

	});



}

function addUser(rowData) {
	var name = rowData.name;
	var userId = rowData.id;

	var point = new BMap.Point(121.553252, 31.245336);

	var myIcon = new BMap.Icon("face.jpg", new BMap.Size(36, 36), {
		anchor: new BMap.Size(18, 57)
	});
	myIcon.setImageSize(new BMap.Size(36, 36));

	//创建marker对象
	var marker = new BMap.Marker(point, {
		icon: myIcon
	});
	var shadowIcon = new BMap.Icon("face_bg.png", new BMap.Size(50, 64), {
		anchor: new BMap.Size(25, 64)
	});
	shadowIcon.setImageSize(new BMap.Size(50, 64));

	marker.setShadow(shadowIcon);
	var label = new BMap.Label(name, {
		offset: new BMap.Size(0, 60)
	});
	marker.setLabel(label);
	label.setStyle({
		color: "#467711",
		fontSize: "14px",
		fontFamily: "微软雅黑",
		border: "none",
		borderRadius: "2px",
		padding: "0px 5px",
		backgroundColor: "rgba(0,0,0,0)",
		fontWeight: "bold"
	});

	marker.setTitle(name);
	marker.setAnimation(BMAP_ANIMATION_DROP);


	var markerJO = {};
	markerJO.id=userId;
	markerJO.name=name;
	markerJO.marker=marker;


	//地图上加标点和路径
	setTrackListByUser(userId,marker,markerJO);

	markerJA.push(markerJO);
}

//remove marker
function removeMarker(id){
	$.each(markerJA,function(i,markerJO){
		if(id == markerJO.id){
			map.removeOverlay(markerJO.marker)
			map.removeOverlay(markerJO.polyline)
		}
	})
}


// 添加缩放控件
var navigationControl = new BMap.NavigationControl({
	// 靠左上角位置
	anchor: BMAP_ANCHOR_TOP_LEFT,
	// LARGE类型
	type: BMAP_NAVIGATION_CONTROL_LARGE,
	// 启用显示定位
	enableGeolocation: true
});
